package com.rockcent.third.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.MerchantCoupon;
import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.dto.IntegrationCouponDetailRespDto;
import com.rockcent.mall.lib.dto.IntegrationCouponReqDto;
import com.rockcent.mall.lib.dto.IntegrationCouponRespDto;
import com.rockcent.service.MallCashCouponSrv;
import com.rockcent.service.MerchantCouponSrv;
import com.rockcent.third.Routes;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 积分优惠券&现金券同步接口
 * Created by zhangjh on 2016/12/30.
 */
@RestController
@RequestMapping(value = Routes.API_PREFIX)
public class IntegrationCouponAPIController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(IntegrationCouponAPIController.class);


    @Autowired
    private MallCashCouponSrv mallCashCouponSrv;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * @api {put} /third/v2/integration/coupon 兑换 卡券
     * @apiGroup integration
     * @apiVersion 1.0.0
     * @apiHeader {String} accessToken 用户Token，第三方积分兑换平台id
     * @apiParam {String} type <code>必选参数</code> 卡券类型 cash("现金券"), coupon("优惠卡券")
     * @apiParam {String} orderNo <code>可选参数</code> 第三方订单号
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": "1",
     * "desc": "成功"
     * }
     */
    @RequestMapping(value = Routes.CGI_BIN_INTEGRATION_COUPON, method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult exchange( @RequestBody IntegrationCouponReqDto dto, @RequestHeader("accessToken") String accessToken, HttpServletRequest request) {

        String channelId = stringRedisTemplate.boundValueOps(UserInfo.REDIS_USER_TOKEN_PREFIX + accessToken).get();

        if (StringUtils.isBlank(channelId)) {
            return new JsonResult<>("10002", "渠道未经过授权");
        }

        RestDoing doing = jr -> {
            if (null == dto) {
                jr.remind("信息有误",log);
                return;
            }
            BigDecimal faceValue =dto.getFaceValue();
            if (null == faceValue) {
                jr.remind("面值信息为空",log);
                return;
            }
            if (StringUtils.isBlank(dto.getOrderNo())) {
                jr.remind("第三方订单号为空",log);
                return;
            }
            if (StringUtils.isBlank(dto.getType())) {
                jr.remind("卡券类型为空",log);
                return;
            }


            IntegrationCouponRespDto couponRespDto = mallCashCouponSrv.exchange(faceValue, channelId, dto);

            jr.data = couponRespDto;


        };
        return doing.go(request, log);
    }



    /**
     * @api {get} /third/v2/integration/coupon/{exchangeCode} 卡券 查询
     * @apiGroup integration
     * @apiVersion 1.0.0
     * @apiHeader {String} accessToken 用户Token，第三方积分兑换平台id
     * @apiParam {String} type <code>必选参数</code> 类型 cash("现金券"), coupon("优惠卡券")
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": "1",
     * "desc": "成功",
     * "data": {
     * "exchangeCode": "f926a7b2f926a7",
     * "flagCode": "0"
     * }
     * }
     */
    @RequestMapping(value = Routes.CGI_BIN_INTEGRATION_COUPON + "{exchangeCode}", method = RequestMethod.GET)
    public JsonResult qrIntegration(IntegrationCouponReqDto reqDto, @PathVariable String exchangeCode, @RequestHeader("accessToken") String accessToken, HttpServletRequest request) {

        String channelId = stringRedisTemplate.boundValueOps(UserInfo.REDIS_USER_TOKEN_PREFIX + accessToken).get();


        if (StringUtils.isBlank(channelId)) {
            return new JsonResult<>("10002", "渠道未经过授权");
        }

        RestDoing doing = jr -> {
            if (null == reqDto) {
                jr.remind("信息有误",log);
                return;
            }
            if (StringUtils.isBlank(reqDto.getType())) {
                jr.remind("卡券类型为空",log);
                return;
            }
            IntegrationCouponDetailRespDto respDto = mallCashCouponSrv.queryInfoByExchangeCodeAndType(exchangeCode, reqDto, channelId);
            jr.data = respDto;
        };
        return doing.go(request, log);
    }
}
